package com.polaris.live.common.util

import android.util.Log
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.polaris.live.common.AppModule
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * log打印
 *
 * @author Created by 半仙 on 2023/8/4/004 9:45
 */
object LogExt {

    const val JSON_INDENT = 2
    const val SINGLE_DIVIDER = "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄"

    init {
        Logger.addLogAdapter(object : AndroidLogAdapter() {
            override fun isLoggable(priority: Int, tag: String?): Boolean {
                return AppModule.isOpenLog()
            }
        })
    }

    fun logStack(tag: String) {
        logI(tag) {
            Log.getStackTraceString(Throwable())
        }
    }

    fun logE(tag: String, msg: String, t: Throwable? = null) {
        if (!AppModule.isOpenLog()) {
            return
        }

        if (t == null) {
            Log.e(tag, msg)
        } else {
            Log.e(tag, msg, t)
        }
    }

    fun logE(tag: String, t: Throwable? = null, block: () -> String) {
        if (!AppModule.isOpenLog()) {
            return
        }

        if (t == null) {
            Log.e(tag, block())
        } else {
            Log.e(tag, block(), t)
        }
    }

    fun logE(tag: String, t: Throwable? = null) {
        if (!AppModule.isOpenLog()) {
            return
        }

        Log.e(tag, "", t)
    }

    fun logI(tag: String, msg: String, vararg args: String) {
        if (!AppModule.isOpenLog()) {
            return
        }

        if (args.isNotEmpty()) {
            Log.i(tag, String.format(msg, *args))
        } else {
            Log.i(tag, msg)
        }
    }

    inline fun logI(tag: String, block: () -> String) {
        if (!AppModule.isOpenLog()) {
            return
        }

        Log.i(tag, block())
    }

    fun logJson(tag: String, block: () -> String, jsonBlock: () -> Array<String>) {
        if (!AppModule.isOpenLog()) {
            return
        }

        logJson(tag, block(), *jsonBlock())
    }

    fun logJson(tag: String, message: String, block: () -> Array<String>) {
        if (!AppModule.isOpenLog()) {
            return
        }

        logJson(tag, message, *block())
    }

    fun logJson(tag: String, message: String, vararg jsons: String?, suffix: Map<Int, String> = emptyMap()) {
        if (!AppModule.isOpenLog()) {
            return
        }

        Logger.t(tag)
        try {
            val sb = StringBuilder()
            if (message.isNotEmpty()) {
                sb.append(message)
            }

            for (i in jsons.indices) {
                val json = jsons[i]
                if (json.isNullOrEmpty()) {
                    continue
                }

                val formatJson = if (json.startsWith("{")) {
                    val jsonObject = JSONObject(json)
                    jsonObject.toString(JSON_INDENT)
                } else if (json.startsWith("[")) {
                    val jsonArray = JSONArray(json)
                    jsonArray.toString(JSON_INDENT)
                } else {
                    json
                }

                sb.append("\n")
                    .append(SINGLE_DIVIDER)
                    .appendSuffix(suffix, i)
                    .append("\n")

                sb.append(formatJson)
            }
            if (sb.isNotEmpty()) {
                Logger.d(sb.toString())
                return
            }
            Logger.e("Invalid Json")
        } catch (e: JSONException) {
            Logger.e("Invalid Json")
        }
    }

    private fun StringBuilder.appendSuffix(suffix: Map<Int, String>, position: Int): StringBuilder {
        suffix[position]?.also {
            append(" ").append(it)
        }

        return this
    }
}

//打印临时日志
fun String.tempLogI() {
    LogExt.logI("--temp") { this }
}